import 'package:base/main.dart';
import 'package:framework/data/datasources/home_local_repository.dart';
import 'package:framework/data/model/function_bean.dart';
import 'package:framework/data/model/menu_bean.dart';
import 'package:framework/main.dart';

class FunctionVm extends BaseViewModel {
  final HomeRepository _repository = HomeLocalRepository();

  /// 金刚区数据rx 首页和更多管理页面共享
  final RxList<FunctionBean> _rxFunctionList = <FunctionBean>[].obs;

  RxList<FunctionBean> get rxFunctionList => _rxFunctionList;

  /// 金刚区接口数据
  List<FunctionBean>? _functionList = [];

  List<FunctionBean>? get functionList => _functionList;

  /// 菜单全数据
  List<MenuBean>? _menuList = [];

  List<MenuBean>? get menuList => _menuList;

  /// 金刚区系统默认数据
  final List<FunctionBean> _systemFunctionList = [];

  List<FunctionBean> get systemFunctionList => _systemFunctionList;

  @override
  Future<void> init() async {
    await _loadFunctionData();
    await _loadMenuData();
  }

  /// 金刚区功能数据获取
  Future<void> _loadFunctionData() async {
    try {
      _functionList?.clear();
      _functionList = await _repository.getFunctionLists();
      _rxFunctionList.clear();
      if (_functionList!.isNotEmpty) {
        List<FunctionBean>? first12 = _functionList?.take(12).toList();
        _rxFunctionList.assignAll(first12!);
      }
      _rxFunctionList.add(
        FunctionBean(name: string_more_manager, icon: "icon_more"),
      );
      Log.i("FunctionVm getFunctionData() ${_functionList?.length}");
    } catch (e) {
      throw DataFailure('DataStat failed: $e');
    }
  }

  /// 更新数据源
  updateData(List<FunctionBean>? moreLists) {
    try {
      _functionList?.assignAll(moreLists!);
      _rxFunctionList.assignAll(moreLists!.take(12));
      _rxFunctionList.add(
        FunctionBean(name: string_more_manager, icon: "icon_more"),
      );
      _repository.saveFunctionList(_functionList!);
    } catch (e) {
      throw DataFailure('DataStat failed: $e');
    }
  }

  /// 菜单
  Future<void> _loadMenuData() async {
    _menuList = await _repository.getMenuLists();
    Log.i("FunctionVm _menuList ${_menuList?.length}");
    _systemFunctionList.clear();
    for (var menu in menuList!) {
      final title = menu.title;
      final data = menu.data;
      for (var datum in data) {
        if (datum.tag == "1") {
          _systemFunctionList.add(
            FunctionBean(
                title: title, name: datum.name, icon: datum.icon, edit: false),
          );
        }
      }
    }
    handlerFunctionData();
    Log.i("FunctionVm _systemFunctionList ${_systemFunctionList.length}");
  }

  handlerFunctionData() {
    if (systemFunctionList.isEmpty) {
      return;
    }
    if (functionList!.isEmpty) {
      _functionList?.assignAll(systemFunctionList);

      List<FunctionBean>? first12 = systemFunctionList.take(12).toList();
      _rxFunctionList.assignAll(first12);
      _rxFunctionList.add(
        FunctionBean(name: string_more_manager, icon: "icon_more"),
      );
      _rxFunctionList.refresh();
    }
  }
}
